﻿using System;
using Brjnk.GpxExplorer.Data;
using Brjnk.GpxExplorer.ViewModel;

namespace Brjnk.GpxExplorer.Workspace.ApplicationStates
{
    public class InsertingToRouteState : InsertingToPathState
    {
        public InsertingToRouteState(string name) : base(name) { }

        public InsertingToRouteState(RouteVM editedObject, RoutePointVM neighbourPoint)
            : base(editedObject, neighbourPoint) { Select(neighbourPoint); }

        public InsertingToRouteState(RouteVM editedObject, RoutePointVM neighbourPoint, InsertDirection direction)
            : base(editedObject, neighbourPoint, direction) { Select(neighbourPoint); }

        protected override object CreatePath()
        {
            var obj = new RouteVM(new RouteDefault());
            App.Instance.Workspace.Data.Routes.Add(obj);
            return obj;
        }

        protected override void DeletePath()
        {
            var obj = GetEditedObject();
            App.Instance.Workspace.Data.Routes.Remove(obj);
        }

        protected override ViewModel.IPoint AddPoint(Common.GpsCoordinates position, ViewModel.IPoint neighbourPoint, InsertDirection direction)
        {
            var obj = GetEditedObject();
            var point = new RoutePointVM(new RoutePointDefault() { Position = position });
            point.IsSelected = App.Instance.Settings.SelectPathPointsDuringEditing;
            var indexOf = obj.Points.IndexOf((RoutePointVM)neighbourPoint);

            if (direction == ApplicationStates.InsertDirection.Before)
            {
                obj.Points.Insert(indexOf, point);
            }
            else if (direction == ApplicationStates.InsertDirection.Behind)
            {
                obj.Points.Insert(indexOf + 1, point);
            }
            else throw new Exception();
            return point;
        }

        protected override void RemovePoint(ViewModel.IPoint toRemove)
        {
            var obj = GetEditedObject();
            obj.Points.Remove((RoutePointVM)toRemove);
        }

        protected override void Complete()
        {
        }

        protected override void Rename(string newName)
        {
            GetEditedObject().Source.Name = newName;
        }

        protected override string GetPointIdentificator(ViewModel.IPoint point)
        {
            var rtePoint = (RoutePointVM)point;
            return rtePoint.Number.ToString();
        }

        private RouteVM GetEditedObject()
        {
            return (RouteVM)base.EditedObject;
        }

        /// <summary>
        /// Vybere předaný a následující bod (pokud je funkcionalita aktivována)
        /// </summary>
        private void Select(RoutePointVM point)
        {
            if (!App.Instance.Settings.SelectPathPointsDuringEditing) return;
            point.IsSelected = true;

            var obj = GetEditedObject();
            var indexOf = obj.Points.IndexOf(point);
            if (indexOf < obj.Points.Count)
                obj.Points[indexOf + 1].IsSelected = true;
        }
    }
}